package com.gc.server.service.impl;

import com.gc.server.dao.PlanDao;
import com.gc.server.dao.TodoDao;
import com.gc.server.dao.UserDao;
import com.gc.server.entity.PlanEntity;
import com.gc.server.entity.TodoEntity;
import com.gc.server.entity.UserEntity;
import com.gc.server.enums.TodoStatusEnum;
import com.yf.response.RRException;
import com.yf.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.gc.server.dao.ScoreDao;
import com.gc.server.entity.ScoreEntity;
import com.gc.server.service.ScoreService;
import com.yf.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 评分Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Tue Mar 10 11:17:45 CST 2020
 */
@Service
public class ScoreServiceImpl extends BaseServiceImpl<ScoreEntity> implements ScoreService {

    private ScoreDao scoreDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private TodoDao todoDao;

    @Autowired
    public void setDao(ScoreDao scoreDao) {
        this.scoreDao = scoreDao;
        super.setBaseDao(scoreDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override
     List<ScoreVo> queryListVo(ScoreVo scoreVo){
     return scoreDao.queryListVo(scoreVo);
     }

     @Override
     ScoreVo getVo(Long id){
     return scoreDao.getVo(id);
     }

     **/


    /**内部方法**/



    /**外部方法实现**/



    @Override
    public List<ScoreEntity> findByTodoId(Long todoId) {
        return scoreDao.findByTodo(todoId);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void score(Long todoId, String[] names, BigDecimal[] points, Long adminId, String[] remarks) {
        UserEntity admin = userDao.get(adminId);
        TodoEntity todoEntity = todoDao.get(todoId);
        UserEntity tem = userDao.get(todoEntity.getUserId());
        if(tem==null||!tem.getPid().equals(adminId)){
            throw new RRException("禁止越级操作!");
        }
        if(!todoEntity.getStatus().equals(TodoStatusEnum.WAIT.getIndex())){
            throw new RRException("禁止操作");
        }
        BigDecimal score = BigDecimal.ZERO;// 总分
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            if(name.contains("Img")){ //图片评分跳过
                continue;
            }
            String remark = remarks[i];
            BigDecimal point = points[i];
            score = score.add(point);
            ScoreEntity scoreEntity = new ScoreEntity(todoId,name,remark,point,todoEntity.getUserId(),admin.getId(),admin.getName());
            scoreDao.save(scoreEntity);
        }
        todoEntity.setStatus(TodoStatusEnum.SCORE.getIndex());
        todoEntity.setScoreTime(new Date());
        todoEntity.setScore(score);
//        calImgScore(todoEntity);
        todoDao.update(todoEntity);
        //更新周计划总分
        updatePlanScore(todoId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TodoEntity preScore(Long todoId, String[] names, BigDecimal[] points,String[] remarks) {
        TodoEntity todoEntity = todoDao.get(todoId);
//        BigDecimal sum = BigDecimal.ZERO;
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            BigDecimal point = points[i];
//            sum= sum.add(point);
            ScoreEntity scoreEntity = new ScoreEntity(todoId,name,remarks[i],point,todoEntity.getUserId(),null,      ShiroUtils.getUserEntity().getUsername());
            scoreDao.save(scoreEntity);
        }
//        todoEntity.setImgScore(sum);
        calImgScore(todoEntity);
        todoEntity.setStatus(TodoStatusEnum.WAIT.getIndex());
        todoEntity.setAdminTime(new Date());
        todoDao.update(todoEntity);
        return todoEntity;
    }

    //计算图片评分
   void calImgScore(TodoEntity todoEntity){
        BigDecimal sum = BigDecimal.ZERO;
        List<ScoreEntity> scores = scoreDao.findByTodo(todoEntity.getId());
       for (ScoreEntity entity: scores) {
           if(entity.getName().contains("Img")){
               if(entity.getPoint()!=null){
                   sum = sum.add(entity.getPoint());
               }
           }
       }
       todoEntity.setImgScore(sum);
   }

    void updatePlanScore(Long todoId){
        TodoEntity todoEntity = todoDao.get(todoId);
        PlanEntity planEntity = planDao.get(todoEntity.getPlanId());
        if(planEntity.getScore()== null){
            planEntity.setScore(todoEntity.getScore());
        }else {
            planEntity.setScore(planEntity.getScore().add(todoEntity.getScore()));
        }
        planDao.update(planEntity);
    }

    @Autowired
    private PlanDao planDao;

}
